{include file="public/header" /}
<body>
<div class="layui-fluid layui-anim layui-anim-fadein">
    {include file="public/referrer" /}
    <form action="{:url('save')}" class="layui-form" lay-filter="form">
        <input type="hidden" name="id" value="<?php echo !empty($info)?$info->id:'';?>">
        <div class="layui-card">
            <div class="layui-card-body">
                <div class="layui-tabs" id="productTabs">
                    <ul class="layui-tabs-header">
                        <li class="layui-this">商品信息</li>
                        <li>商品设置</li>
                        <li>商品图片</li>
                        <li>商品详情</li>
                    </ul>
                    <div class="layui-tabs-body">
                        <div class="layui-tabs-item layui-show">
                            <div class="layui-row">
                                {:builderFormInit([
                                    'title' => '商品名称',
                                    'name' => 'title',
                                    'value' => !empty($info) ? $info->title : '',
                                    'req' => true,
                                    'md' => 12
                                ])->getFormItemInputHtml()}

                                <?php echo builderFormInit([
                                    'title' => '所属专区',
                                    'name' => 'type_id',
                                    'options' => \app\common\model\ProductType::column('title', 'id'),
                                    'value' => !empty($info) ? $info->type_id : ''
                                ])->getFormItemSelectHtml('type_id');?>

                                <div class="layui-linkage"></div>
                                {:builderFormInit([
                                    'title' => '虚拟销量',
                                    'name' => 'virtual_sales',
                                    'value' => !empty($info) ? $info->virtual_sales : '',
                                    'md' => 12,
                                    'remark' => '如若填写，前台销量将展示虚拟销量+实际销量'
                                ])->getFormItemInputHtml('number')}

                                <?php echo builderFormInit([
                                'title' => '供应商',
                                'name' => 'supplier_id',
                                'options' => \app\common\model\Supplier::column('title', 'id'),
                                'value' => !empty($info) ? $info->supplier_id : ''
                                ])->getFormItemSelectHtml('supplier_id');?>

                                {:builderFormInit([
                                    'title' => '排序',
                                    'name' => 'sort',
                                    'value' => !empty($info) ? $info->sort : '',
                                    'md' => 12
                                ])->getFormItemInputHtml('number')}
                            </div>
                        </div>
                        <div class="layui-tabs-item">
                            <!-- 引入规格组件 -->
                            <div class="layui-spec layui-row"></div>
                        </div>
                        <div class="layui-tabs-item">
                            <div class="layui-row">
                                {:builderFormInit([
                                    'title' => '商品图片',
                                    'name' => 'images',
                                    'value' => !empty($info) ? $info->images : '',
                                    'remark' => '建议尺寸：800*800，默认首张图为主图，最多上传10张，拖动图片可排序'
                                ])->getFormItemImgHtml(10)}
                            </div>
                        </div>
                        <div class="layui-tabs-item">
                            <div class="layui-row">
                                {:builderFormInit([
                                    'title' => '详情',
                                    'name' => 'content',
                                    'value' => !empty($info) ? $info->content : '',
                                    'md' => 12
                                ])->getFormItemEditorHtml()}
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div style="clear: both;height: 120px;"></div>
        <div class="layui-footer">
            <button class="layui-btn" lay-filter="saveProduct" lay-submit>保存</button>
        </div>
    </form>
</div>
</body>
{include file="public/footer" /}
<script>
    layui.use(['edit', 'spec', 'methods', 'linkage'], function () {
        var spec = layui.spec,
            edit = layui.edit,
            methods = layui.methods,
            linkage = layui.linkage,
            form = layui.form,
            ly = layui.layer,
            o = layui.jquery
        ;
        //渲染edit组件
        edit.render();

        var typeId = "{$info?$info.type_id:''}";
        if(typeId === '5'){
            o(".show-platoon-id").show();
        }else{
            o(".show-platoon-id").hide();
        }

        //渲染联动组件
        /*linkage.render({
            url: '{:url("productCate/getCate")}',
            deep: 2,
            label: '所属分类',
            map: {
                active: ["{$info->cate_1 ?? ''}", "{$info->cate_2 ?? ''}"],
                title: ['一级分类', '二级分类'],
                name: ['cate_1', 'cate_2'],
            }
        });*/

        //渲染Spec组件
        spec.render({
            getSpecOptions: "{:url('product/getSpecOptions')}",
            getSpec: "{:url('spec/getSpec')}",
            uploads: "{:url('uploads/image')}",
            alone: false
        });

        //表单提交
        form.on('submit(saveProduct)', () => {
            let fVal = form.val('form');
            fVal.specData = spec.fun.getSpecData();
            fVal.spec = spec.fun.getSpec();
            methods.load()
            methods.fetchThrottle("{:url('save')}", fVal, (e) => {
                methods.closeLoad()
                e.code === 1 ? ly.msg(e.msg, {time: 1000}, () => {
                    location.href = document.referrer;
                }) : ly.msg(e.msg);
            }, (e) => {
                methods.closeLoad()
                ly('加载失败');
            })
            return false;
        });

    });
</script>

</html>